package com.hongning.railway.ui.categorylistnews;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.app.Constant;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.reqbean.ReqNewsBean;
import com.hongning.railway.entity.resbean.DataBeanX;
import com.hongning.railway.entity.resbean.ResNewsBean;
import com.hongning.railway.ui.main.fragments.home.HomeViewModel;
import com.hongning.railway.ui.main.fragments.home.itemviewmodel.NewItemViewModel;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class CategoryListNewsFragmentViewModel extends BaseViewModel<AppRepository> {
    private int page = 1;
    public ItemBinding<NewItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.news_item);
    public UIChangeData uc = new UIChangeData();
    public ObservableList<NewItemViewModel> viewlist = new ObservableArrayList<>();
    private String cateId;

    public void setCateId(String cateId) {
        this.cateId = cateId;
    }

    public BindingCommand onRefreshCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            page = 1;
            if (TextUtils.isEmpty(cateId)) {
                requestNewsListbytime();
            } else {
                requestNewsList(cateId);
            }

        }
    });

    public BindingCommand onLoadMoreCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (TextUtils.isEmpty(cateId)) {
                requestNewsListbytime();
            } else {
                requestNewsList(cateId);
            }
        }
    });

    public CategoryListNewsFragmentViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }

    public void requestNewsListbytime() {
        ReqNewsBean reqNewsBean = new ReqNewsBean();
        reqNewsBean.setPageSize(Constant.PAGE_SIZE);
        reqNewsBean.setPageNum(page);
        reqNewsBean.setOrderByCreateTime("1");
        reqNewsBean.setNewsCategory("1");
        model.getNewsList(reqNewsBean).compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                    }
                }).doOnTerminate(new Action() {
            @Override
            public void run() throws Exception {
                dismissDialog();
            }
        }).subscribe(new ApiDisposableObserver<DataBeanX>() {
            @Override
            public void onResult(DataBeanX dataBeanX) {
                if (page == 1) {
                    uc.finishRefreshing.call();
                    viewlist.clear();
                } else {
                    uc.finishLoadmore.call();
                }
                if (dataBeanX.getData().size() < Constant.PAGE_SIZE) {
                    uc.enableLoadmore.call();
                }
                if (page == 1 && dataBeanX.getData().isEmpty()) {
                    uc.emptyList.call();
                }
                page++;
                for (ResNewsBean resNewsBean : dataBeanX.getData()) {
                    viewlist.add(new NewItemViewModel(CategoryListNewsFragmentViewModel.this, resNewsBean));
                }
            }
        });
    }

    public class UIChangeData {
        //下拉刷新完成
        public SingleLiveEvent finishRefreshing = new SingleLiveEvent<>();
        //上拉加载完成
        public SingleLiveEvent finishLoadmore = new SingleLiveEvent<>();
        //是否已经到最后
        public SingleLiveEvent enableLoadmore = new SingleLiveEvent<>();
        //列表为空的情况
        public SingleLiveEvent emptyList = new SingleLiveEvent<>();
    }

    public void requestNewsList(String cateid) {
        ReqNewsBean reqNewsBean = new ReqNewsBean();
        reqNewsBean.setNewsTypeId(cateid);
        reqNewsBean.setPageSize(Constant.PAGE_SIZE);
        reqNewsBean.setPageNum(page);
        reqNewsBean.setNewsCategory("1");
        model.getNewsList(reqNewsBean).compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                    }
                }).doOnTerminate(new Action() {
            @Override
            public void run() throws Exception {
                dismissDialog();
            }
        }).subscribe(new ApiDisposableObserver<DataBeanX>() {
            @Override
            public void onResult(DataBeanX dataBeanX) {
                if (page == 1) {
                    uc.finishRefreshing.call();
                    viewlist.clear();
                } else {
                    uc.finishLoadmore.call();
                }
                if (dataBeanX.getData().size() < Constant.PAGE_SIZE) {
                    uc.enableLoadmore.call();
                }
                if (page == 1 && dataBeanX.getData().isEmpty()) {
                    uc.emptyList.call();
                }
                page++;
                for (ResNewsBean resNewsBean : dataBeanX.getData()) {
                    viewlist.add(new NewItemViewModel(CategoryListNewsFragmentViewModel.this, resNewsBean));
                }
            }
        });
    }
}
